package com.banciyuan.bcywebview.base.view.foldview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.common.UIUtils;

/**
 * Created by squall on 2016/9/20.
 */

public class FoldLayout extends RelativeLayout {

    private OnFoldComplate onFoldComplate;
    private int stable_height;
    private int max;
    private int mem_Y;
    private float radius;
    private int mem_last;
    private int over_height;
    private int total_height;

    private static final int STATE_FOLD = 0;
    private static final int STATE_UNFOLD = 1;
    private static final int STATE_UNFOLDCOMPLATE = 2;
    private int state;

    public int getMax() {
        return max;
    }

    public FoldLayout(Context context) {
        super(context);
    }

    public FoldLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            getAttr(attrs, context);
        }
    }

    public FoldLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (!isInEditMode()) {
            getAttr(attrs, context);
        }
    }

    public FoldLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        if (!isInEditMode()) {
            getAttr(attrs, context);
        }
    }


    private void getAttr(AttributeSet attrs, Context context) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Fold);
        stable_height = (int) a.getDimension(R.styleable.Fold_stable_height, UIUtils.dip2px(48, getContext()));
        radius = 9 / 16;
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            max = MeasureSpec.getSize(heightMeasureSpec) - stable_height;
        } catch (Exception e) {
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            max = MeasureSpec.getSize(heightMeasureSpec) - stable_height;
        }
    }

    public void changeHeader(int scrollY) {
        if (mem_Y - scrollY < -max) {
            mem_Y = -max;
        } else if (mem_Y - scrollY > 0) {
            mem_Y = 0;
        } else {
            mem_Y -= scrollY;
        }
        this.setTranslationY(mem_Y);
        if (onFoldComplate != null) {
            if (state == STATE_UNFOLD && mem_Y == -max) {
                onFoldComplate.folded();
                state = STATE_FOLD;
            } else if (state == STATE_FOLD && mem_Y > -max) {
                onFoldComplate.unfolder();
                state = STATE_UNFOLD;
            } else {
                onFoldComplate.folding((float) mem_Y / -max, -mem_Y);
            }
        }
    }

    public void setOnFoldComplate(OnFoldComplate onFoldComplate) {
        this.onFoldComplate = onFoldComplate;
    }

    public void processTransY(FoldChild foldChild) {
        foldChild.getParentHeight(mem_Y);
    }

    public interface OnFoldComplate {
        void folded();

        void unfolder();

        void folding(float percent, int height);
    }

    public interface FoldChild {
        void getParentHeight(int height);

        void passParentScrollY(OnFoldScrollListener onFoldScrollListener);
    }

    public interface OnFoldScrollListener {
        void onScroll(int Y);

        void onTop();
    }

    public void reset() {
        mem_Y = 0;
        setTranslationY(0);
    }

    public void setStable_height(int stable_height) {
        this.stable_height = stable_height;
    }
}
